//
//  KylinPresentationController.swift
//  Stauts
//
//  Created by Kylin on 16/3/14.
//  Copyright © 2016年 Kylin. All rights reserved.
//

import UIKit

class KylinPresentationController: UIPresentationController {

    // 属性
    var presentedFrame : CGRect = CGRectZero
    private lazy var coverView : UIView = UIView()
    
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
     
        // 1.设置弹出的View的尺寸和位置
        presentedView()?.frame = presentedFrame
        // 添加蒙板
        setupCoverView()
        
    }
}

extension KylinPresentationController {
    private func setupCoverView () {
        //1:设置蒙板属性
        coverView.frame = (containerView?.bounds)!
        coverView.backgroundColor = UIColor(white: 0.8, alpha: 0.2)
        
        // 2 :监听蒙板点击
        let tapGes = UITapGestureRecognizer(target: self, action: #selector(KylinPresentationController.coverViewClick))
        coverView.addGestureRecognizer(tapGes)
        
        // 3:将蒙板添加到容器的view中
        containerView?.insertSubview(coverView, belowSubview: presentedView()!)
    }
    
    @objc private func coverViewClick () {
        
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
}